#include "camera.h"
#include "stdlib.h"
#include "keyboard.h"
#include "platform.h"
#include "histogram.h"
#include "core.h"
#include "lang.h"
#include "conf.h"
#include "math.h"
#include "gui.h"
#include "gui_draw.h"
#include "gui_lang.h"
#include "gui_batt.h"
#include "gui_osd.h"


short min_aind, max_aind, sel_aind;
short sel_frame_blink;


//-------------------------------------------------------------------
typedef struct {
    int     title;
    OSD_pos *pos;
    OSD_pos size;
} OSD_elem;

static OSD_elem osd[]={
    {LANG_OSD_LAYOUT_EDITOR_HISTO,      &conf.histo_pos,        {HISTO_WIDTH+2, HISTO_HEIGHT}   },
    {LANG_OSD_LAYOUT_EDITOR_MISC,       &conf.values_pos,       {12*FONT_WIDTH, 9*FONT_HEIGHT}   },
    {LANG_OSD_LAYOUT_EDITOR_BAT_ICON,   &conf.batt_icon_pos,    {31, 12}                        },
    {LANG_OSD_LAYOUT_EDITOR_BAT_TEXT,   &conf.batt_txt_pos,     {5*FONT_WIDTH, FONT_HEIGHT}     },
    {LANG_OSD_LAYOUT_EDITOR_CLOCK,      &conf.clock_pos,        {5*FONT_WIDTH, FONT_HEIGHT}     },
    {LANG_OSD_LAYOUT_EDITOR_EV,     &conf.mode_ev_pos,   {12*FONT_WIDTH, FONT_HEIGHT}   },
    {0}
};
static int osd_to_draw;
static int curr_item;
static char osd_buf[64];
static char osd_buf2[10];
static char osd_buf3[10];
static char osd_buf4[10];

static int step;

int new_meas;

EXPO_TYPE expo;

#define OSD_STATE    0
#define OSD_MISC     1

unsigned char clip8(signed short x){ if (x<0) x=0; else if (x>255) x=255; return x; }

//-------------------------------------------------------------------
void gui_osd_init() {
    osd_to_draw = 1;
    curr_item = 0;
    step = 2;
    draw_restore();
}

//-------------------------------------------------------------------
void gui_osd_draw() {
    if (osd_to_draw) {
        int i;
        draw_restore();
        gui_osd_draw_histo();
        gui_batt_draw_osd();
        gui_osd_draw_values();
        gui_osd_draw_clock(0,0,0);
        for (i=1; i<=2; ++i) {
            draw_rect((osd[curr_item].pos->x>=i)?osd[curr_item].pos->x-i:0, (osd[curr_item].pos->y>=i)?osd[curr_item].pos->y-i:0,
                      osd[curr_item].pos->x+osd[curr_item].size.x+i-1, osd[curr_item].pos->y+osd[curr_item].size.y+i-1,
                      COLOR_GREEN);
        }
        sprintf(osd_buf, " %s:  x:%d y:%d s:%d ", lang_str(osd[curr_item].title), osd[curr_item].pos->x, osd[curr_item].pos->y, step);
        draw_string(0, (osd[curr_item].pos->x<strlen(osd_buf)*FONT_WIDTH+4 && osd[curr_item].pos->y<FONT_HEIGHT+4)?screen_height-FONT_HEIGHT:0,
                    osd_buf, MAKE_COLOR(COLOR_RED, COLOR_WHITE));
        osd_to_draw = 0;
    }
}

//-------------------------------------------------------------------
void gui_osd_kbd_process() {
    switch (kbd_get_autoclicked_key()) {
    case KEY_LEFT:
        if (osd[curr_item].pos->x > 0) {
            osd[curr_item].pos->x-=(osd[curr_item].pos->x>=step)?step:osd[curr_item].pos->x;
            osd_to_draw = 1;
        }
        break;
    case KEY_RIGHT:
        if (osd[curr_item].pos->x < screen_width-osd[curr_item].size.x) {
            osd[curr_item].pos->x+=(screen_width-osd[curr_item].size.x-osd[curr_item].pos->x>step)?step:screen_width-osd[curr_item].size.x-osd[curr_item].pos->x;
            osd_to_draw = 1;
        } else
            osd[curr_item].pos->x = screen_width-osd[curr_item].size.x;
        break;
    case KEY_UP:
        if (osd[curr_item].pos->y > 0) {
            osd[curr_item].pos->y-=(osd[curr_item].pos->y>=step)?step:osd[curr_item].pos->y;
            osd_to_draw = 1;
        }
        break;
    case KEY_DOWN:
        if (osd[curr_item].pos->y < screen_height-osd[curr_item].size.y) {
            osd[curr_item].pos->y+=(screen_height-osd[curr_item].size.y-osd[curr_item].pos->y>step)?step:screen_height-osd[curr_item].size.y-osd[curr_item].pos->y;
            osd_to_draw = 1;
        } else
            osd[curr_item].pos->y = screen_height-osd[curr_item].size.y;
        break;
    case KEY_SET:
        ++curr_item;
        if (!osd[curr_item].pos)
            curr_item = 0;
        osd_to_draw = 1;
        break;
    case KEY_DISPLAY:
        step=(step==1)?10:1;
        osd_to_draw = 1;
        break;
    }
}

//-------------------------------------------------------------------
static void gui_osd_draw_single_histo(int hist, coord x, coord y, int small) {
    register unsigned int i, v, threshold;
    register color cl, cl_over, cl_bg=conf.histo_color>>8;
    coord w=HISTO_WIDTH, h=HISTO_HEIGHT;

    switch (hist) {
        case HISTO_R:
            cl=((mode_get()&MODE_MASK) == MODE_REC)?COLOR_HISTO_R:COLOR_HISTO_R_PLAY;
            break;
        case HISTO_G:
            cl=((mode_get()&MODE_MASK) == MODE_REC)?COLOR_HISTO_G:COLOR_HISTO_G_PLAY;
            break;
        case HISTO_B:
            cl=((mode_get()&MODE_MASK) == MODE_REC)?COLOR_HISTO_B:COLOR_HISTO_B_PLAY;
            break;
        case HISTO_RGB:
        case HISTO_Y:
        default:
            cl=conf.histo_color;
            break;
    }

    if (small) {
        h>>=1; w>>=1;
        for (i=0; i<w; ++i) {
            threshold = (histogram[hist][i<<1]+histogram[hist][(i<<1)+1])>>2;

            for (v=0; v<h-1; ++v)
                draw_pixel(x+1+i, y+h-v, (v<=threshold)?cl:cl_bg);
            cl_over = (threshold==h && conf.show_overexp)?conf.histo_color2>>8:cl;
            for (; v<=h; ++v)
                draw_pixel(x+1+i, y+h-v, (v<=threshold)?cl_over:cl_bg);
        }
    } else {
        for (i=0; i<w; ++i) {
            threshold = histogram[hist][i];

            for (v=0; v<h-3; ++v)
                draw_pixel(x+i, y+h-v, (v<=threshold)?cl:cl_bg);
            cl_over = (threshold==h && conf.show_overexp)?conf.histo_color2>>8:cl;
            for (; v<=h; ++v)
                draw_pixel(x+i, y+h-v, (v<=threshold)?cl_over:cl_bg);
        }
    }

    //draw_rect(x, y, x+1+w, y+h, conf.histo_color2&0xFF);
    //Vertical Lines
    if (conf.histo_show_ev_grid) for (i=1;i<=4;i++) draw_line(x+(1+w)*i/5, y, x+(1+w)*i/5, y+h, conf.histo_color2&0xFF);
}



//-------------------------------------------------------------------
int draw_guard_pixel() {
    unsigned char* buffer1 = vid_get_bitmap_fb()+screen_buffer_size/2;
    unsigned char* buffer2 = buffer1+screen_buffer_size;
    int has_disappeared=0;

    if(*buffer1!=COLOR_GREEN) has_disappeared=1;
    if(*buffer2!=COLOR_GREEN) has_disappeared=2;
    *buffer1 = *buffer2 = COLOR_GREEN;
    return has_disappeared;
}



//-------------------------------------------------------------------
static void gui_osd_draw_blended_histo(coord x, coord y) {
    register unsigned int i, v, red, grn, blu, sel;
    int m = ((mode_get()&MODE_MASK) == MODE_REC);
    color cls[] = {
        conf.histo_color>>8,
        (m)?COLOR_HISTO_B:COLOR_HISTO_B_PLAY,
        (m)?COLOR_HISTO_G:COLOR_HISTO_G_PLAY,
        (m)?COLOR_HISTO_BG:COLOR_HISTO_BG_PLAY,
        (m)?COLOR_HISTO_R:COLOR_HISTO_R_PLAY,
        (m)?COLOR_HISTO_RB:COLOR_HISTO_RB_PLAY,
        (m)?COLOR_HISTO_RG:COLOR_HISTO_RG_PLAY,
        COLOR_WHITE
    };

    for (i=0; i<HISTO_WIDTH; ++i) {
        red = histogram[HISTO_R][i];
        grn = histogram[HISTO_G][i];
        blu = histogram[HISTO_B][i];

        for (v=0; v<=HISTO_HEIGHT; ++v) {
            sel = 0;

            if (v < red) sel = 4;
            if (v < grn) sel |= 2;
            if (v < blu) sel |= 1;

            draw_pixel(x+i, y+HISTO_HEIGHT-v, cls[sel]);
        }
    }

    //draw_rect(x, y, x+1+HISTO_WIDTH, y+HISTO_HEIGHT, conf.histo_color2&0xFF);
    //Vertical lines
    if (conf.histo_show_ev_grid) for (i=1;i<=4;i++) draw_line(x+(1+HISTO_WIDTH)*i/5, y, x+(1+HISTO_WIDTH)*i/5, y+HISTO_HEIGHT, conf.histo_color2&0xFF);

}

void gui_osd_draw_histo() {
    switch (conf.histo_layout) {
        case OSD_HISTO_LAYOUT_Y:
                gui_osd_draw_single_histo(HISTO_Y, conf.histo_pos.x, conf.histo_pos.y, 0);
                break;
        case OSD_HISTO_LAYOUT_A_Y:
                gui_osd_draw_single_histo(HISTO_RGB, conf.histo_pos.x, conf.histo_pos.y, 0);
                gui_osd_draw_single_histo(HISTO_Y, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT, 0);
                break;
        case OSD_HISTO_LAYOUT_R_G_B:
                gui_osd_draw_single_histo(HISTO_R, conf.histo_pos.x, conf.histo_pos.y, 0);
                gui_osd_draw_single_histo(HISTO_G, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT, 0);
                gui_osd_draw_single_histo(HISTO_B, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT*2, 0);
                break;
        case OSD_HISTO_LAYOUT_A_yrgb:
                gui_osd_draw_single_histo(HISTO_RGB, conf.histo_pos.x, conf.histo_pos.y, 0);
                gui_osd_draw_single_histo(HISTO_Y, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT, 1);
                gui_osd_draw_single_histo(HISTO_R, conf.histo_pos.x+HISTO_WIDTH/2+1, conf.histo_pos.y+HISTO_HEIGHT, 1);
                gui_osd_draw_single_histo(HISTO_G, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT+HISTO_HEIGHT/2, 1);
                gui_osd_draw_single_histo(HISTO_B, conf.histo_pos.x+HISTO_WIDTH/2+1, conf.histo_pos.y+HISTO_HEIGHT+HISTO_HEIGHT/2, 1);
                break;
        case OSD_HISTO_LAYOUT_Y_argb:
                gui_osd_draw_single_histo(HISTO_Y, conf.histo_pos.x, conf.histo_pos.y, 0);
                gui_osd_draw_single_histo(HISTO_RGB, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT, 1);
                gui_osd_draw_single_histo(HISTO_R, conf.histo_pos.x+HISTO_WIDTH/2+1, conf.histo_pos.y+HISTO_HEIGHT, 1);
                gui_osd_draw_single_histo(HISTO_G, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT+HISTO_HEIGHT/2, 1);
                gui_osd_draw_single_histo(HISTO_B, conf.histo_pos.x+HISTO_WIDTH/2+1, conf.histo_pos.y+HISTO_HEIGHT+HISTO_HEIGHT/2, 1);
                break;
        case OSD_HISTO_LAYOUT_BLEND:
                gui_osd_draw_blended_histo(conf.histo_pos.x, conf.histo_pos.y);
                break;
        case OSD_HISTO_LAYOUT_BLEND_Y:
                gui_osd_draw_blended_histo(conf.histo_pos.x, conf.histo_pos.y);
                gui_osd_draw_single_histo(HISTO_Y, conf.histo_pos.x, conf.histo_pos.y+HISTO_HEIGHT, 0);
                break;
        case OSD_HISTO_LAYOUT_A:
        default:
                gui_osd_draw_single_histo(HISTO_RGB, conf.histo_pos.x, conf.histo_pos.y, 0);
                break;
    }

    if (conf.histo_layout != OSD_HISTO_LAYOUT_R_G_B) {
        if (under_exposed && conf.show_overexp) {
            draw_filled_ellipse(conf.histo_pos.x+5, conf.histo_pos.y+5, 3, 3, MAKE_COLOR(conf.histo_color2>>8, conf.histo_color2>>8));
        }

        if (over_exposed && conf.show_overexp) {
            draw_filled_ellipse(conf.histo_pos.x+HISTO_WIDTH-5, conf.histo_pos.y+5, 3, 3, MAKE_COLOR(conf.histo_color2>>8, conf.histo_color2>>8));
        }
    }

    if ((conf.show_overexp ) && kbd_is_key_pressed(KEY_SHOOT_HALF) && (under_exposed || over_exposed))
      draw_string(conf.histo_pos.x+HISTO_WIDTH-FONT_WIDTH*3, conf.histo_pos.y-FONT_HEIGHT, "EXP", conf.histo_color);

}

//-------------------------------------------------------------------
static void sprintf_dist(char *buf, float dist) {
// length of printed string is always 4
    if (dist<=0 || dist>=MAX_DIST) {
        sprintf(buf, " inf");
    } else if (dist<1000) {
        sprintf(buf, ".%03d", (int)dist);
    } else if (dist<10000) {
        sprintf(buf, "%d.%02d", (int)(dist/1000), (int)(dist/10)%100);
    } else if (dist<100000) {
        sprintf(buf, "%02d.%d", (int)(dist/1000), (int)(dist/100)%10);
    } else {
        sprintf(buf, "%4d", (int)(dist/1000));
    }
}

static void sprintf_canon_values(char *buf, short dist)
{
short v=((dist<0)?-dist:dist);
sprintf(buf, "%s%d.%02d", ((dist<0)?"-":""), v/96, v%96);
}


//-------------------------------------------------------------------

void gui_osd_calc_expo_param() {

    expo.av96=shooting_get_av96();
    expo.tv96=shooting_get_tv96();
    expo.sv96=shooting_get_sv96();
    expo.iso=shooting_get_iso_real();
    expo.sv96_market=shooting_get_svm96();
    expo.iso_market=shooting_get_iso_market();
    expo.bv96_measured=shooting_get_bv96();

    if (expo.ev96_seted != expo.tv96+expo.av96)
    {
        expo.ev96_seted=expo.tv96+expo.av96; //Tv96+Av96
        
        expo.ev96_measured_prev2 = expo.ev96_measured_prev1;
        expo.ev96_measured_prev1 = expo.ev96_measured;
        
        expo.ev96_measured=expo.bv96_measured+expo.sv96;//Bv96+Sv96
        
        new_meas = 0;
    }

    expo.dev96=expo.ev96_measured-expo.ev96_seted;// Ev96_external-Ev96_internal
    expo.bv96_seted=expo.ev96_seted-expo.sv96;
    expo.dev96_canon=shooting_get_canon_overexposure_value();
    expo.b=shooting_get_luminance();
}


static short n, m; //string number

void gui_print_osd_state_string_int(const char * title, int value) {
  strcpy(osd_buf, title);
  sprintf(osd_buf+strlen(osd_buf), "%d", value);
  sprintf(osd_buf+strlen(osd_buf), "%12s", "");
  osd_buf[12]=0;
  draw_string(conf.mode_state_pos.x, conf.mode_state_pos.y+n, osd_buf, conf.osd_color_override);
  n+=FONT_HEIGHT;
}

void gui_print_osd_state_string_chr(const char *title, const char *value) {
  strcpy(osd_buf, title);
  sprintf(osd_buf+strlen(osd_buf), "%s", value);
  sprintf(osd_buf+strlen(osd_buf), "%12s", "");
  osd_buf[12]=0;
  draw_string(conf.mode_state_pos.x, conf.mode_state_pos.y+n, osd_buf, conf.osd_color_override);
  n+=FONT_HEIGHT;
}

void gui_print_osd_state_string_float(const char * title, const char * fmt, int divisor, int value) {
  strcpy(osd_buf, title);
  sprintf(osd_buf+strlen(osd_buf), fmt, (int)(value/divisor), (int)(value%divisor));
  sprintf(osd_buf+strlen(osd_buf), "%12s", "");
  osd_buf[12]=0;
  draw_string(conf.mode_state_pos.x, conf.mode_state_pos.y+n, osd_buf, conf.osd_color_override);
  n+=FONT_HEIGHT;
}

void gui_print_osd_misc_string_int(const char * title, int value) {
  strcpy(osd_buf, title);
  sprintf(osd_buf+strlen(osd_buf), "%d", value);
  sprintf(osd_buf+strlen(osd_buf), "%12s", "");
  osd_buf[12]=0;
  draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
  m+=FONT_HEIGHT;
}

/*
void gui_print_osd_misc_string_float(const char * title, const char * fmt, int divisor, int value) {
  char s[16];
  strcpy(osd_buf, title);
  sprintf(s, fmt, (int)(value/divisor), (int)(value%divisor));
  sprintf(osd_buf+strlen(osd_buf), "%6s", s);
  //osd_buf[8]=0;
  draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
  m+=FONT_HEIGHT;
}
*/

void gui_print_osd_misc_string_float(const char * title, const char * fmt, int divisor, int value) {
  strcpy(osd_buf, title);
  sprintf(osd_buf+strlen(osd_buf), fmt, (int)(value/divisor), (int)(value%divisor));
  sprintf(osd_buf+strlen(osd_buf), "%12s", "");
  osd_buf[12]=0;
  draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
  m+=FONT_HEIGHT;
}

/*
void gui_print_osd_misc_string_dist(const char * title, int value) {
  strcpy(osd_buf, title);
  sprintf_dist(osd_buf+strlen(osd_buf), (float)value);
  sprintf(osd_buf+strlen(osd_buf), "%9s", "");
  osd_buf[9]=0;
  draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
  m+=FONT_HEIGHT;
}
*/

void gui_print_osd_dof_string_dist(const char * title, int value, short use_good_color) {
  strcpy(osd_buf, title);
  int i=strlen(osd_buf);
  if (i<8) {
    draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
    sprintf_dist(osd_buf, (float)value);
    sprintf(osd_buf+strlen(osd_buf), "%9s", "");
    osd_buf[9-i]=0;
    draw_string(conf.values_pos.x+i*FONT_WIDTH, conf.values_pos.y+m, osd_buf, use_good_color?((conf.osd_color & 0xff00) | COLOR_GREEN):conf.osd_color);
  } else {
    osd_buf[9]=0;
    draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
  }
  m+=FONT_HEIGHT;
}

void gui_print_osd_misc_string_canon_values(const char * title, short value) {
  strcpy(osd_buf, title);
  sprintf_canon_values(osd_buf+strlen(osd_buf), value);
  sprintf(osd_buf+strlen(osd_buf), "%12s", "");
  osd_buf[12]=0;
  draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
  m+=FONT_HEIGHT;
}


//-------------------------------------------------------------------
void gui_osd_draw_values(int showtype)
{
m = 0;
        if (conf.save_frame_info && !kbd_is_key_pressed(KEY_SHOOT_HALF))
        {
            sprintf(osd_buf, "%2d", conf.sel_frame);
            if (sel_frame_blink)
            {
                draw_string(conf.clock_pos.x, conf.histo_pos.y+HISTO_HEIGHT-FONT_HEIGHT+1, osd_buf, (sel_frame_blink&1)?conf.menu_title_color:conf.osd_color_warn);
                
                if (--sel_frame_blink == 0)
                    if (conf.sel_frame < 12)
                    {
                        conf.sel_frame++;
                        conf_save();
                    }
                
            }
            else
                draw_string(conf.clock_pos.x, conf.histo_pos.y+HISTO_HEIGHT-FONT_HEIGHT+1, osd_buf, conf.osd_color_warn);
        }


        static char *apertures[] = { "2.8", "3.5", "4", "-", "5.6", "-", "8", "-", "11", "-", "16", "-", "22", 0 };
        static char *speeds[] = { "1/2000", "1/1500", "1/1000", "1/750", "1/500", "1/375", "1/250", "1/190", "1/125", "1/90", "1/60", "1/45", "1/30", "1/20", "1/15", "1/12", "1/8", "1/6", "1/4", "1/3", "1/2", "1/1.5", "1s", "1.5s", "2s", "3s", "4s", "6s", "8s", 0 };
        static char *comps[6]={"    ", "1/6 ", "1/3 ", "1/2 ", "2/3 ", "5/6 "};

        int set_ev = expo.ev96_seted;
        int set_eva = set_ev>=0 ? set_ev : -set_ev;
        int meas_ev = expo.ev96_measured;
        int meas_eva = meas_ev>=0 ? meas_ev : -meas_ev;
        int comp_ev = shooting_get_prop(PROPCASE_EV_CORRECTION_1);
        int comp_eva = comp_ev>=0 ? comp_ev : -comp_ev;
        
        int meas_ev_prev1 = expo.ev96_measured_prev1;
        int meas_ev_prev1a = meas_ev_prev1>=0 ? meas_ev_prev1 : -meas_ev_prev1 ;
        int meas_ev_prev2 = expo.ev96_measured_prev2;
        int meas_ev_prev2a = meas_ev_prev2>=0 ? meas_ev_prev2 : -meas_ev_prev2 ;
        
        int meas_ev_avg = 0;
        int meas_ev_avg_cnt = 0;
        
        if (meas_ev_prev1)
        {
            meas_ev_avg += meas_ev_prev1;
            meas_ev_avg_cnt++;
        }
        if (meas_ev_prev2)
        {
            meas_ev_avg += meas_ev_prev2;
            meas_ev_avg_cnt++;
        }
        meas_ev_avg += meas_ev;
        meas_ev_avg_cnt++;
        
        meas_ev_avg /= meas_ev_avg_cnt;
        int meas_ev_avga = meas_ev_avg>=0 ? meas_ev_avg : -meas_ev_avg ;
        
        //int set_ev100 = expo.ev96_seted + (int)(0.62148*96);
        //int meas_ev100 = expo.ev96_measured + (int)(0.62148*96);
        
        static char comp_buf[10];
        if (comp_ev/96 || comp_ev == 0)
            sprintf(comp_buf, "%c%d %s", (comp_ev>=0?'+':'-'), comp_eva/96, comps[comp_eva/16%6]);
        else
            sprintf(comp_buf, "%c%s  ", (comp_ev>=0?'+':'-'), comps[comp_eva/16%6]);

        sprintf(osd_buf, "%d.%02d <- %d.%02d %d.%02d %d.%02d %s=%d.%02d",
                meas_ev_avg/96, (int)(((float)(meas_ev_avga%96))/96*100),
                meas_ev_prev2/96, (int)(((float)(meas_ev_prev2a%96))/96*100),
                meas_ev_prev1/96, (int)(((float)(meas_ev_prev1a%96))/96*100),
                meas_ev/96, (int)(((float)(meas_eva%96))/96*100),
                comp_buf,
                set_ev/96, (int)(((float)(set_eva%96))/96*100));
        
        draw_string(conf.values_pos.x-(39-12)*FONT_WIDTH, conf.values_pos.y+m, "     ", 0);
        draw_string(conf.values_pos.x-(strlen(osd_buf)-12)*FONT_WIDTH, conf.values_pos.y+m, osd_buf, conf.osd_color);

        m += FONT_HEIGHT + 3;

        /*if (conf.expo_type)
            draw_string(conf.values_pos.x-4*FONT_WIDTH, conf.values_pos.y+m, "AVG", conf.osd_color_warn);
        else
            draw_string(conf.values_pos.x-4*FONT_WIDTH, conf.values_pos.y+m, "   ", 0);*/
        
        /*long (*GetShutterSpeedRegularMaxLimit)() = 0xFFE01BA0;
        long (*GetShutterSpeedRegularMinLimit)() = 0xFFE01BB8;
        
        long a = GetShutterSpeedRegularMinLimit();
        long b = GetShutterSpeedRegularMaxLimit();
        sprintf (osd_buf, "%d %d", a, b);
        draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
        m+=FONT_HEIGHT;*/

        /*if (comp_ev/96 || comp_ev == 0)
            sprintf(osd_buf, "%d.%02d %c%d %s=%d.%02d", meas_ev100/96, (int)(((float)(meas_ev100%96))/96*100), (comp_ev>=0?'+':'-'), comp_eva/96, comps[comp_eva/16%6], set_ev100/96, (int)(((float)(set_ev100%96))/96*100));
        else
            sprintf(osd_buf, "%d.%02d %c%s  =%d.%02d", meas_ev100/96, (int)(((float)(meas_ev100%96))/96*100), (comp_ev>=0?'+':'-'), comps[comp_eva/16%6], set_ev100/96, (int)(((float)(set_ev100%96))/96*100));
        
        draw_string(conf.values_pos.x-(strlen(osd_buf)-12)*FONT_WIDTH, conf.values_pos.y+m, osd_buf, conf.osd_color);
        m+=FONT_HEIGHT;*/
        
        /*int a =  *(char*)0xffd5f3d0;
        int b = *(char*)0xffd5f3e0;
        
        sprintf(osd_buf, "%d %d", a, b);
        draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, conf.osd_color);
        
        *(char*)0xffd5f3d0 = 0x48;
        *(char*)0xffd5f3e0 = 0x48;
        
        m+=FONT_HEIGHT;*/

        if (!kbd_is_key_pressed(KEY_SHOOT_HALF))
        {
            min_aind = -1;
            max_aind = -1;
            
            int expo_ev;
            if (conf.expo_type)
                expo_ev = meas_ev_avg;
            else
                expo_ev = set_ev;
    
            int aind;
            for (aind = 1; apertures[aind]; aind++)
            {
                int evd = 14*96 + aind*48 - expo_ev;
    
                int evdi = evd / 48;
                int evdr = evd % 48;
    
                if (evdr > 24)
                    evdi++;
    
                if (evdi >= 0 && evdi <= 28)
                {
                    if (min_aind == -1)
                        min_aind = aind;
                    max_aind = aind;
                        
                    sprintf(osd_buf, "%3s : %s %9s", apertures[aind], speeds[evdi], "");
                    osd_buf[12] = 0;
                    draw_string(conf.values_pos.x, conf.values_pos.y+m, osd_buf, ((sel_aind == aind && conf.save_frame_info) || conf.expo_type) ? conf.osd_color_warn : conf.osd_color);
                    m+=FONT_HEIGHT;
                }
            }
    
            if (sel_aind < min_aind)
                sel_aind = min_aind;
            else if (sel_aind > max_aind)
                sel_aind = max_aind;
        }
}

#define CLOCK_FORMAT_24 0
#define CLOCK_FORMAT_12 1
#define CLOCK_WITHOUT_SEC 1
#define CLOCK_WITH_SEC 2

//-------------------------------------------------------------------
void gui_osd_draw_clock(int x, int y, color cl) {
    unsigned long t;
    static struct tm *ttm;
    int w = 0;
    int z;
    static char am[4];
    static char pm[4];
    static char curr[4];
    t = time(NULL);
    ttm = localtime(&t);
    unsigned int hour=(ttm->tm_hour);
    if (conf.clock_format == CLOCK_FORMAT_12) {
     switch(conf.clock_indicator)
      {
        case 1:
            sprintf(pm, "P");
            sprintf(am, "A");
            w = 1;
            break;
        case 2:
            sprintf(pm, ".");
            sprintf(am, " ");
            w = 1;
            break;
        default:
            sprintf(pm, " PM");
            sprintf(am, " AM");
            w = 3;
            break;
      }
     sprintf(curr,((hour>=12)?pm:am));
     if ((ttm->tm_hour)==00)  hour=12;
     else if ((ttm->tm_hour)>12)  hour=hour-12;
    }

        if (conf.clock_format == CLOCK_FORMAT_24)
          sprintf(osd_buf, "%02u:%02u", hour, ttm->tm_min);
        else
          sprintf(osd_buf, "%2u:%02u%s", hour, ttm->tm_min,curr);
        z=0;

    if ((conf.clock_format==CLOCK_FORMAT_12) && (conf.clock_pos.x>=(z+w)*FONT_WIDTH) )
       draw_string((x)?x-(z+w)*FONT_WIDTH:conf.clock_pos.x-(z+w)*FONT_WIDTH, (y)?y:conf.clock_pos.y, osd_buf, (cl)?cl:conf.osd_color);
    else
       draw_string((x)?x:conf.clock_pos.x, (y)?y:conf.clock_pos.y, osd_buf, (cl)?cl:conf.osd_color);
}





void gui_osd_draw_ev() {
    static char *s[6]={"   ", "1/6", "1/3", "1/2", "2/3", "5/6"};
    short ev=shooting_get_prop(PROPCASE_EV_CORRECTION_1);
    if (ev/96 || (ev==0)) sprintf(osd_buf, "EV:  %d %s", abs(ev/96), s[abs(ev/16%6)]);
    else sprintf(osd_buf, "EV:  %s   ", s[abs(ev/16%6)]);
    if (ev>0) osd_buf[4]='+'; else if (ev<0) osd_buf[4]='-';
    draw_string(conf.mode_ev_pos.x, conf.mode_ev_pos.y, osd_buf, conf.osd_color);

}


